#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
@Author: kindey
@Date: 2025/8/12
@Description: 
"""
import logging
import traceback
from django.views import View
from apps.utils.response_utils import success_response, error_response
from apps.services.plt_services import PlotService

class PltPlotDevView(View):
    """
    绘制设备折线图
    """
    logging.basicConfig(level=logging.DEBUG)
    logger = logging.getLogger(__name__)

    def get(self, request):
        try:
            dev_id1 = request.GET.get('dev_id1', None)
            dev_id2 = request.GET.get('dev_id2', None)
            dev_ids_str = request.GET.get('dev_ids')
            dev_ids = [int(id) for id in dev_ids_str.split(',')]
            start_time = request.GET.get('start_time', None)
            end_time = request.GET.get('end_time', None)
            is_save_file = request.GET.get('is_save_file', None)
            opt = request.GET.get('opt', None)
            if opt == 'one':
                file_path = PlotService.plot_one_dev(dev_id1, start_time, end_time, is_save_file)
            elif opt == 'two':
                file_path = PlotService.plot_two_dev(dev_id1, dev_id2, start_time, end_time, is_save_file)
            elif opt == 'more':
                file_path = PlotService.plot_more_dev(dev_ids, start_time, end_time, is_save_file)
            else:
                return error_response(msg='请选择绘制设备折线图选项')
            return success_response(data=file_path)
        except Exception as e:
            error_traceback = traceback.format_exc()
            self.logger.error(f"Exception occurred: {str(e)}\nTraceback:\n{error_traceback}")
            return error_response(str(e))
